home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / mac / files / t_sys5 / 92052tar.gz / 920528.tar / sys5unix.c < prev    next >
C/C++ Source or Header  |  1992-03-31  |  10KB  |  491 lines

  1. #include <sys/types.h>
  2.  
  3. #include <signal.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <ctype.h>
  7. #include <sys/stat.h>
  8. #include <time.h>
  9. #include <termio.h>
  10. #include <unistd.h>
  11. #include <sys/bsdtypes.h>
  12. #ifdef SPASSWD
  13. #include <shadow.h>
  14. #endif
  15. #include <sys/fs/s5dir.h>
  16. #include <sys/user.h>
  17.  
  18. #include "sys5unix.h"
  19. #include "global.h"
  20. #include "iface.h"
  21. #include "timer.h"
  22. #include "files.h"
  23. #include "hardware.h"
  24. #include "main.h"
  25. #include "hpux.h" 
  26.  
  27. extern int  Debug;
  28. extern long  sigsetmask();
  29. extern void _exit();
  30. void abort();
  31.  
  32. #define TIMEOUT    200
  33.  
  34. static int  chkread[2];
  35. static int  actread[2];
  36. static void (*readfnc[_NFILE]) __ARGS((void *));
  37. static void *readarg[_NFILE];
  38.  
  39. static int  chkwrite[2];
  40. static int  actwrite[2];
  41. static void (*writefnc[_NFILE]) __ARGS((void *));
  42. static void *writearg[_NFILE];
  43.  
  44. static int  chkexcp[2];
  45. static int  actexcp[2];
  46. static void (*excpfnc[_NFILE]) __ARGS((void *));
  47. static void *excparg[_NFILE];
  48.  
  49. static int  nfds = -1;
  50.  
  51. static int  local_kbd;
  52.  
  53. static struct termio curr_termio;
  54. static struct termio prev_termio;
  55.  
  56. static void check_files_changed __ARGS((void));
  57.  
  58. /*---------------------------------------------------------------------------*/
  59. void ioinit()
  60. {
  61.  
  62.   int i;
  63.  
  64. #define fixdir(name, mode) \
  65.     { mkdir((name), (mode)); chmod((name), (mode)); }
  66.  
  67.   fixdir("/tcp", 0755);
  68.   fixdir("/tcp/sockets", 0755);
  69.   fixdir("/tcp/.sockets", 0700);
  70.   fixdir("/tcp/logs", 0700);
  71.  
  72.   if (local_kbd = isatty(0)) {
  73.     ioctl(0, TCGETA, &prev_termio);
  74.     curr_termio = prev_termio;
  75.     curr_termio.c_iflag = BRKINT | ICRNL | IXON | IXANY | IXOFF;
  76.     curr_termio.c_lflag = 0;
  77.     curr_termio.c_cc[VMIN] = 0;
  78.     curr_termio.c_cc[VTIME] = 0;
  79.     ioctl(0, TCSETA, &curr_termio);
  80.     fflush(stdout);
  81.     on_read(0, (void (*)()) keyboard, (void *) 0);
  82.   } else {
  83.     for (i = 0; i < _NFILE; i++) close(i);
  84.     setpgrp();
  85.     fopen("/dev/null", "r+");
  86.     fopen("/dev/null", "r+");
  87.     fopen("/dev/null", "r+");
  88.     remote_kbd_initialize();
  89.   }
  90.   setvbuf(stdout, NULL, _IOLBF, BUFSIZ); 
  91.   signal(SIGPIPE, SIG_IGN);
  92.   signal(SIGALRM, abort); 
  93.   signal(SIGCLD, sigchild_handler);  
  94.   if (!Debug) alarm(TIMEOUT);
  95.   umask(022);
  96.   if (!getenv("HOME"))
  97.     putenv("HOME=/users/root");
  98.   if (!getenv("LOGNAME"))
  99.     putenv("LOGNAME=root");
  100.   if (!getenv("PATH"))
  101.     putenv("PATH=/bin:/usr/bin:/usr/contrib/bin:/usr/local/bin");
  102.   if (!getenv("SHELL"))
  103.     putenv("SHELL=/bin/sh");
  104.   if (!getenv("TZ"))
  105.     putenv("TZ=MEZ-1MESZ");
  106.   {
  107.     /* Init times */
  108.     struct timeval tv;
  109.     struct timezone tz;
  110.     gettimeofday(&tv, &tz);
  111.     Secclock = tv.tv_sec;
  112.     Msclock = 1000 * Secclock + tv.tv_usec / 1000;
  113.   }
  114.   fixutmpfile();
  115. }
  116.  
  117. /*---------------------------------------------------------------------------*/
  118.  
  119. void iostop()
  120. {
  121.   register struct iface *ifp;
  122.  
  123.   if (local_kbd) {
  124.     ioctl(0, TCSETA, &prev_termio);
  125.     fflush(stdout);
  126.   }
  127.   for (ifp = Ifaces; ifp; ifp = ifp->next)
  128.     if (ifp->stop) (*ifp->stop)(ifp);
  129. }
  130.  
  131. /*---------------------------------------------------------------------------*/
  132. int system(cmdline)
  133. const char *cmdline;
  134. {
  135.  
  136.   int  i, pid, status;
  137.   long  oldmask;
  138.  
  139.   if (!cmdline) return 1;
  140.  switch (pid = fork()) {
  141.   case -1:
  142.     return (-1);
  143.   case 0:
  144.     for (i = 3; i < _NFILE; i++) close(i);
  145.     execl("/bin/sh", "sh", "-c", cmdline, (char *) 0);
  146.     _exit(127); 
  147.   default:
  148.     while (wait3(&status, -1, NULL) != pid)
  149.     sleep(-15); /* Wait some ticks */
  150.     return status; 
  151.   }
  152. }
  153.  
  154. /*---------------------------------------------------------------------------*/
  155.  
  156. int  _system(cmdline)
  157. char  *cmdline;
  158. {
  159.   return system(cmdline);
  160. }
  161.  
  162. /*---------------------------------------------------------------------------*/
  163.  
  164. int  doshell(argc, argv, p)
  165. int  argc;
  166. char  *argv[];
  167. void *p;
  168. {
  169.   char  buf[2048];
  170.  
  171.   *buf = '\0';
  172.   while (--argc > 0) {
  173.     if (*buf) strcat(buf, " ");
  174.     strcat(buf, *++argv);
  175.   }
  176.   return system(buf);
  177. }
  178.  
  179. /*---------------------------------------------------------------------------*/
  180.  
  181. #define setmask(mask, fd) ((mask)[(fd)>>5] |=  (1 << ((fd) & 31)))
  182. #define clrmask(mask, fd) ((mask)[(fd)>>5] &= ~(1 << ((fd) & 31)))
  183. #define maskset(mask, fd) ((mask)[(fd)>>5] &   (1 << ((fd) & 31)))
  184.  
  185. /*---------------------------------------------------------------------------*/
  186.  
  187. void on_read(fd, fnc, arg)
  188. int  fd;
  189. void (*fnc) __ARGS((void *));
  190. void *arg;
  191. {
  192.   readfnc[fd] = fnc;
  193.   readarg[fd] = arg;
  194.   setmask(chkread, fd);
  195.   clrmask(actread, fd);
  196.   nfds = -1;
  197. }
  198.  
  199. /*---------------------------------------------------------------------------*/
  200.  
  201. void off_read(fd)
  202. int  fd;
  203. {
  204.   readfnc[fd] = 0;
  205.   readarg[fd] = 0;
  206.   clrmask(chkread, fd);
  207.   clrmask(actread, fd);
  208.   nfds = -1;
  209. }
  210.  
  211. /*---------------------------------------------------------------------------*/
  212.  
  213. void on_write(fd, fnc, arg)
  214. int  fd;
  215. void (*fnc) __ARGS((void *));
  216. void *arg;
  217. {
  218.   writefnc[fd] = fnc;
  219.   writearg[fd] = arg;
  220.   setmask(chkwrite, fd);
  221.   clrmask(actwrite, fd);
  222.   nfds = -1;
  223. }
  224.  
  225. /*---------------------------------------------------------------------------*/
  226.  
  227. void off_write(fd)
  228. int  fd;
  229. {
  230.   writefnc[fd] = 0;
  231.   writearg[fd] = 0;
  232.   clrmask(chkwrite, fd);
  233.   clrmask(actwrite, fd);
  234.   nfds = -1;
  235. }
  236.  
  237. /*---------------------------------------------------------------------------*/
  238.  
  239. void on_excp(fd, fnc, arg)
  240. int  fd;
  241. void (*fnc) __ARGS((void *));
  242. void *arg;
  243. {
  244.   excpfnc[fd] = fnc;
  245.   excparg[fd] = arg;
  246.   setmask(chkexcp, fd);
  247.   clrmask(actexcp, fd);
  248.   nfds = -1;
  249. }
  250.  
  251. /*---------------------------------------------------------------------------*/
  252.  
  253. void off_excp(fd)
  254. int  fd;
  255. {
  256.   excpfnc[fd] = 0;
  257.   excparg[fd] = 0;
  258.   clrmask(chkexcp, fd);
  259.   clrmask(actexcp, fd);
  260.   nfds = -1;
  261. }
  262.  
  263. /*---------------------------------------------------------------------------*/
  264.  
  265. static void check_files_changed()
  266. {
  267.  
  268.   int  changed = 0;
  269.   static long  nexttime, net_time, rc_time;
  270.   struct stat statbuf;
  271.  
  272.   if (Debug || nexttime > secclock()) return;
  273.   nexttime = secclock() + 600;
  274.  
  275.   if (stat("/tcp/net", &statbuf)) return;
  276.   if (!net_time) net_time = statbuf.st_mtime;
  277.   if (net_time != statbuf.st_mtime && statbuf.st_mtime < secclock() - 3600)
  278.     changed = 1;
  279.  
  280.   if (stat(Startup, &statbuf)) return;
  281.   if (!rc_time) rc_time = statbuf.st_mtime;
  282.   if (rc_time != statbuf.st_mtime && statbuf.st_mtime < secclock() - 3600)
  283.     changed = 1;
  284.  
  285.   if (changed) doexit(0, (char **) 0, (void *) 0);
  286. }
  287.  
  288. /*---------------------------------------------------------------------------*/
  289.  
  290. void eihalt()
  291. {
  292.  
  293.   int  n;
  294.   int  status;
  295.   int32 nte;
  296.   register unsigned int  i;
  297.   struct timeval timeout;
  298.  
  299.   check_files_changed();
  300.   wait3(&status, WNOHANG, (int *) 0);      
  301.   if (!Debug) alarm(TIMEOUT);
  302.   if (nfds < 0) {
  303.     if (i = chkread[1] | chkwrite[1] | chkexcp[1])
  304.       nfds = 32;
  305.     else {
  306.       i = chkread[0] | chkwrite[0] | chkexcp[0];
  307.       nfds = 0;
  308.     }
  309.     for (n = 16; n; n >>= 1)
  310.       if (i & ((-1) << n)) {
  311.     nfds += n;
  312.     i >>= n;
  313.       }
  314.     if (i) nfds++;
  315.   }
  316.   actread [0] = chkread [0];
  317.   actread [1] = chkread [1];
  318.   actwrite[0] = chkwrite[0];
  319.   actwrite[1] = chkwrite[1];
  320.   actexcp [0] = chkexcp [0];
  321.   actexcp [1] = chkexcp [1];
  322.   timeout.tv_sec = 0;
  323.   if (Hopper)
  324.     timeout.tv_usec = 0;
  325.   else {
  326.     nte = next_timer_event();
  327.     if (nte > 999) nte = 999;
  328.     timeout.tv_usec = 1000 * nte;
  329.   }
  330.   if (select(nfds, actread, actwrite, actexcp, &timeout) < 1) {
  331.     actread [0] = actread [1] = 0;
  332.     actwrite[0] = actwrite[1] = 0;
  333.     actexcp [0] = actexcp [1] = 0;
  334.   } else
  335.     for (n = nfds - 1; n >= 0; n--) {
  336.       if (readfnc [n] && maskset(actread , n)) (*readfnc [n])(readarg [n]);
  337.       if (writefnc[n] && maskset(actwrite, n)) (*writefnc[n])(writearg[n]);
  338.       if (excpfnc [n] && maskset(actexcp , n)) (*excpfnc [n])(excparg [n]);
  339.     }
  340. }
  341.  
  342.  
  343. /* some other things */
  344.  
  345. #ifdef ISC2
  346.  
  347. rename(s1, s2)
  348. char *s1, *s2;
  349. {
  350.     char tmp[50];
  351.     int i;
  352.     unlink(s2);
  353.     i = link(s1, s2);
  354.     if(i == 0){
  355.             unlink(s1);
  356.     }
  357.     return i;
  358. }
  359.  
  360. #endif
  361.  
  362. /* wait stuff */
  363.  
  364. #define MAXZOMBIE 64
  365. struct zombie {
  366.     int pid;
  367.     int status;
  368. } zombie [MAXZOMBIE];
  369.  
  370. int wait3 (statloc, options, dummy)
  371.     int *statloc;
  372.     int options;
  373.     int *dummy; {
  374.     int i, pid;
  375.     
  376.     do {
  377.         for (i = 0; i < MAXZOMBIE; i++)
  378.             if (zombie[i].pid) {
  379.                 if (statloc)
  380.                     *statloc = zombie[i].status;
  381.                 pid = zombie[i].pid;
  382.                 zombie[i].pid = 0;
  383.                 return pid;
  384.             }
  385.     } while (!options);
  386.     return 0;
  387. }
  388.  
  389. static
  390. void
  391. sigchild_handler(sig, code)
  392.     int sig;
  393.     int code; {
  394.     int victim, status, i;
  395.     victim = wait (&status);
  396.     signal (sig, sigchild_handler);
  397.     for (i = 0; i < MAXZOMBIE; i++)
  398.         if (!zombie[i].pid) {
  399.             zombie[i].status = status;
  400.             zombie[i].pid = victim;
  401.             return;
  402.         }
  403. }
  404. #ifdef SPASSWD
  405. /** /etc/shadow processing **/
  406.  
  407. struct spwd *getspwdentry(name)
  408. char  *name;
  409. {
  410.  
  411. #define DEFAULTUSER    "guest"
  412.  
  413.   FILE * fp;
  414.   char  *cp;
  415.   char  username[128];
  416.   int  fd;
  417.   int  uid;
  418.   struct spwd *sw;
  419.  
  420.   /* Fix user name */
  421.  
  422.   for (cp = username; isalnum(uchar(*name)); *cp++ = tolower(uchar(*name++))) ;
  423.   *cp = '\0';
  424.   if (!isalpha(uchar(*username)) || strlen(username) > 8)
  425.     strcpy(username, DEFAULTUSER);
  426.  
  427.   /* Search existing shadow entry */
  428.  
  429.   while ((sw = getspent()) && strcmp(username, sw->sp_namp)) ;
  430.   endspent();
  431.   if (sw) return sw;
  432.   return 0;
  433. }
  434. #endif 
  435.  
  436. #ifndef RESTRICTED  /* ISC with bug allowing to set the u_uid etc. to 0 */
  437. int 
  438. setresuid(r,e,s)
  439. int r,e,s;
  440. {
  441.     struct user *user;
  442.     
  443.     user = (struct user *) 0xE0000000;
  444.  
  445.     user->u_uid = r;
  446.     user->u_ruid = e;
  447.     return 0;
  448. }
  449. int
  450. setresgid(r,e,s)
  451. int r,e,s;
  452. {
  453.     
  454.     struct user *user;
  455.     
  456.     user = (struct user *) 0xE0000000;
  457.  
  458.     user->u_gid = r;
  459.     user->u_rgid = e;
  460.     return 0;
  461. }
  462. #else
  463.  
  464. int setresuid(r,e,s)
  465. int r,e,s;
  466. {
  467. }
  468.  
  469. int setresgid(r,e,s)
  470. int r,e,s;
  471. {
  472. }
  473.  
  474. #endif /* RESTRICTED */
  475. /* Some dummy functions */
  476.  
  477. unsigned long strtoul (str, ptr, base)
  478. char *str, **ptr;
  479. int base;
  480. {
  481. return strtol(str, ptr, base);
  482. }
  483. rtprio()
  484. {
  485. }
  486. #ifndef ISC3
  487. settimeofday()
  488. {
  489. }
  490. #endif
  491.